package com.dxp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxp.common.BaseContext;
import com.dxp.common.CustomException;
import com.dxp.common.R;
import com.dxp.pojo.ShoppingCart;
import com.dxp.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置id,判断是那个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",shoppingCart.getUserId());
        //判断添加的菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        //判断添加的是菜品还是套餐
        if(dishId!=null){
            //菜品
            queryWrapper.eq("dish_id",dishId);
        }else {
            //套餐
            queryWrapper.eq("setmeal_id",shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
        if(shoppingCart1!=null){
            //如果在,则把数量加1
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number+1);
            shoppingCartService.updateById(shoppingCart1);
        }else {
            //如果不在，则添加该数据，把数量设置为1
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            shoppingCart1=shoppingCart;
        }
        return R.success(shoppingCart1);
    }
    //展示购物车数据
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        queryWrapper.orderByDesc("create_time");
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCarts);
    }
    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id",BaseContext.getCurrentId());
        shoppingCartService.remove(shoppingCartQueryWrapper);
        return R.success("清空成功");
    }
    //减数量
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //设置id,判断是那个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",shoppingCart.getUserId());
        Long dishId = shoppingCart.getDishId();
        //判断减少的是菜品还是套餐
        if(dishId!=null){
            //菜品
            queryWrapper.eq("dish_id",dishId);
        }else {
            //套餐
            queryWrapper.eq("setmeal_id",shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);

        Integer number = shoppingCart1.getNumber();
        shoppingCart1.setNumber(number-1);
        shoppingCartService.updateById(shoppingCart1);
        if(shoppingCart1.getNumber()==0){
            shoppingCartService.removeById(shoppingCart1);
        }
        return R.success(shoppingCart1);
    }

}
